/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-21，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.db;

import java.util.HashMap;
import java.util.Map;

import com.chinamobile.ln.ictenterprises.cache.Cache;
import com.chinamobile.ln.ictenterprises.common.Config;
import com.chinamobile.ln.ictenterprises.utils.Utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DataBaseImpl implements IDataBase
{

    private DBUtils db = null;
    private static IDataBase database;

    private DataBaseImpl(Context context)
    {
        db = new DBUtils(context);
        db.open();
    }

    public static IDataBase getInstance(Context context)
    {
        if (database == null)
        {
            database = new DataBaseImpl(context);
        }
        return database;
    }

    @Override
    public synchronized boolean saveOrupdateCache(String url,
        ContentValues contentValues)
    {
        Cursor cursor = null;
        try
        {
            cursor = db.query(Cache.TABLE_NAME, null, "url" + "='" + url + "'");
            if (cursor != null && cursor.getCount() > 0)
            {
                return db.update(Cache.TABLE_NAME, "url" + "='" + url + "'",
                    contentValues);
            }
            else
            {
                return db.insert(Cache.TABLE_NAME, contentValues) != -1;
            }
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            if (cursor != null)
                cursor.close();
        }
    }

    @Override
    public void deleteOutTimeCache()
    {
        long time = 0;
        try
        {
            db.delete(Cache.TABLE_NAME, Cache.CACHETIME + " > '" + time + "'");
        }
        catch (Exception e)
        {}
        finally
        {}
    }

    @Override
    public Object queryHttpCache(String url)
    {
        Cursor cursor = null;
        try
        {
            cursor = db
                .query(Cache.TABLE_NAME, new String[] { Cache.URL,
                    Cache.CACHEMAP, Cache.CACHETIME }, Cache.URL + "='" + url
                    + "'");
            if (cursor.getCount() > 0)
            {
                @SuppressWarnings("unchecked")
                Map<String, Object> objMap = (HashMap<String, Object>) Utils
                    .serialOut(cursor.getBlob(1));
                String cacheUpdateTime = cursor.getString(2);
                objMap.put(Config.CACHE_TIME, cacheUpdateTime);
                return objMap;
            }
            else
            {
                return null;
            }

        }
        catch (Exception e)
        {}
        finally
        {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    @Override
    public boolean deleteHttpCache(String url)
    {
        if (url == null)
        {
            return false;
        }
        try
        {
            return db.delete(Cache.TABLE_NAME, Cache.URL + "='" + url + "'");
        }
        catch (Exception e)
        {
            return false;
        }
        finally
        {}
    }
}
